'============================================================================================================
'	CheckVAShorts
'	Author:		Sizzl (cm[at]phore.net)
'	Modifed:	25/10/2010 - Initial build
'
'	Script Function:
'		Check virtual app arrShortcuts which have been manually copied from their original location
'		and are no longer within management scope
'	
'	Usage:
'		cscript //nologo //b CheckVAShorts.vbs
'	
'	License:
'	
'	Licensed under the Apache License, Version 2.0 (the "License");
'	you may not use this file except in compliance with the License.
'	You may obtain a copy of the License at
'	
'	http://www.apache.org/licenses/LICENSE-2.0
'	
'	Unless required by applicable law or agreed to in writing, software
'	distributed under the License is distributed on an "AS IS" BASIS,
'	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
'	See the License for the specific language governing permissions and
'	limitations under the License.
'============================================================================================================

On Error Resume Next

Dim strThisLocation: strThisLocation = Left(WScript.ScriptFullName,InStrRev(WScript.ScriptFullName,"\"))
Dim objShell: Set objShell = CreateObject("WScript.Shell")
Dim objFSO: Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim strParentLocation: strParentLocation = objFSO.GetParentFolderName(strThisLocation)
Dim strComputer: strComputer = "."
Dim WshNetwork: Set WshNetwork = WScript.CreateObject("WScript.Network"): strComputer = WshNetwork.ComputerName
'============================================================================================================
Dim arrShortcuts(50)
Dim i: i = 0
If objFSO.FileExists(objShell.ExpandEnvironmentStrings("%TEMP%") & "\vapps.log") then objFSO.DeleteFile objShell.ExpandEnvironmentStrings("%TEMP%") & "\vapps.log"
Set oFolder = objFSO.GetFolder(objShell.ExpandEnvironmentStrings("%USERPROFILE%") & "\Desktop")
For Each oFile in oFolder.Files
	If LCase(Right(oFile.Name,"4")) = ".lnk" Then 
		Set oShortcut = objShell.CreateShortcut(objFSO.GetAbsolutePathName(oFile))
		if InStr(LCase(oShortcut.targetpath),"sfttray.exe") then
			strLaunchApp = oShortcut.Arguments
			strLaunchApp = Replace(Right(strLaunchApp,Len(strLaunchApp)-8),"""","")
			arrShortcuts(i) = array(oFile.Name,strLaunchApp,0,"",objFSO.GetAbsolutePathName(oFile))
			i = i + 1
		end if
	End If
Next
Set oFolder = objFSO.GetFolder(objShell.ExpandEnvironmentStrings("%APPDATA%") & "\Microsoft\Internet Explorer\Quick Launch")
For Each oFile in oFolder.Files
	If LCase(Right(oFile.Name,"4")) = ".lnk" Then 
		Set oShortcut = objShell.CreateShortcut(objFSO.GetAbsolutePathName(oFile))
		if InStr(LCase(oShortcut.targetpath),"sfttray.exe") then
			strLaunchApp = oShortcut.Arguments
			strLaunchApp = Replace(Right(strLaunchApp,Len(strLaunchApp)-8),"""","")
			arrShortcuts(i) = array(oFile.Name,strLaunchApp,0,"",objFSO.GetAbsolutePathName(oFile))
			i = i + 1
		end if
	End If
Next
Set oFolder = Nothing
Dim j: j = i - 1
If NOT objFSO.FileExists(objShell.ExpandEnvironmentStrings("%TEMP%") & "\vapps.log") then ' Generate
	WScript.Echo "Generating Virtual App List..."
	objShell.Run "cmd /c sftmime /query obj:app /short /log " & objShell.ExpandEnvironmentStrings("%TEMP%") & "\vapps.log",0,true
	If objFSO.FileExists(objShell.ExpandEnvironmentStrings("%TEMP%") & "\vapps.log") then
		Dim objTS: Set objTS = objFSO.OpenTextFile(objShell.ExpandEnvironmentStrings("%TEMP%") & "\vapps.log",1,false)
		Do While objTS.AtEndOfStream = false
			strLine = objTS.ReadLine
			For i = 0 to j
				If InStr(strLine, arrShortcuts(i)(1)) then
					arrShortcuts(i)(2) = 1
				Else
					strCompare = Left(arrShortcuts(i)(1),InStrRev(arrShortcuts(i)(1)," ")-1)
					if InStr(strLine,strCompare) then ' best bet
						arrShortcuts(i)(3) = strLine
					end if
				End If
			Next
		Loop
		objTS.Close
		Set objTS = Nothing
	End If
End If
For i = 0 to j
	If arrShortcuts(i)(2) = 0 then
		Set oShortcut = objShell.CreateShortcut(arrShortcuts(i)(4))
		if InStr(LCase(oShortcut.targetpath),"sfttray.exe") then
			oShortcut.Arguments = "/launch """ & arrShortcuts(i)(3) & """"
			oShortcut.Save
			WSCript.Echo "Updated shortcut: " & arrShortcuts(i)(0) & ", closest match: " & arrShortcuts(i)(3)
		end if
	End If
Next

